package com.yuer.rpc.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 抽象类
 */
public class NettyHttpServerHander extends SimpleChannelInboundHandler<HttpObject> {

    public static List<Channel> list = new ArrayList<>();


//    @Override
//    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
//       if()(){
//
//        }
//    }


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {
        if(httpObject instanceof HttpRequest){
            HttpRequest httpRequest = (HttpRequest) httpObject;
            System.out.println("浏览器的请求路径： "+httpRequest.uri());
            ByteBuf byteBuf = Unpooled.copiedBuffer("hello,我是netty服务端,", CharsetUtil.UTF_8);
            DefaultFullHttpResponse response= new  DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,byteBuf);
            //对其设置响应头  ；类型和长度
            response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/html;charset=utf-8");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes());
            channelHandlerContext.writeAndFlush(response);
        }
    }
}
